const axios = require('../utils/http').axiosInstance;

class Messages {
    static async send(peerId, text) {
        const randomId = Math.floor(Math.random() * 100_000),
              uriEncodedText = encodeURIComponent(text);

        const res = await axios.post(`/messages.send?peer_id=${peerId}&random_id=${randomId}&message=${uriEncodedText}&access_token=${process.env.ACCESS_TOKEN}&v=${process.env.API_VERSION}`),
              data = JSON.parse(res.data);

        if('error' in data) throw new Error(data.error.error_msg);
    };
};

module.exports = Messages;
